/*
    This file is part of Manhunt.

    Manhunt is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Manhunt is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Manhunt.  If not, see <https://www.gnu.org/licenses/>.
 */

package snw.manhunt;

import org.bukkit.*;
import org.bukkit.entity.EnderDragon;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import snw.manhunt.managers.GameManager;

public final class EventProcessor implements Listener {
    @EventHandler
    public void onJoin(PlayerJoinEvent event) {
        Player player = event.getPlayer();
        if (GameManager.isGameStarted() && !(GameManager.getRunner() == player && GameManager.getHunters().contains(player.getName()))) {
            player.sendMessage("游戏已开始，因此你现在是旁观者。");
            event.getPlayer().setGameMode(GameMode.SPECTATOR);
        }
    }

    @EventHandler
    public void onMove(PlayerMoveEvent event) {
        if (!GameManager.isGameStarted()) return;
        if (GameManager.getHunters().contains(event.getPlayer().getName()) && !GameManager.isHunterSetOut()) event.setCancelled(true);
        if (GameManager.getRunner() == event.getPlayer()) for (Player i : event.getPlayer().getWorld().getPlayers()) i.setCompassTarget(event.getPlayer().getLocation());
    }

    @EventHandler
    public void onPlayerDeath(PlayerDeathEvent event) {
        if (!(GameManager.isGameStarted() && event.getEntity() == GameManager.getRunner())) return;
        GameManager.stopGame();
        Bukkit.broadcastMessage(ChatColor.RED + "" + "速通者死亡！猎人胜利！");
        for (String o : GameManager.getHunters()) {
            if (GameManager.removeIfNotOnline(o)) continue;
            Player i = Bukkit.getPlayerExact(o);
            //noinspection ConstantConditions
            i.playSound(i.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, SoundCategory.MASTER, 10, 1);
            i.sendTitle(ChatColor.GREEN + "" + "你赢了！", "", 10, 70, 20);
        }
    }

    @EventHandler
    public void onEnderDragonDeath(EntityDeathEvent event) {
        if (!(event.getEntity() instanceof EnderDragon)) return;
        GameManager.stopGame();
        Player runner = GameManager.getRunner();
        if (runner != null) {
            runner.sendTitle("你赢了！", "", 10, 70, 20);
            runner.playSound(runner.getLocation(), Sound.UI_TOAST_CHALLENGE_COMPLETE, SoundCategory.MASTER, 10, 1);
        }
        Bukkit.broadcastMessage(ChatColor.RED + "" + "末影龙死亡！速通者胜利！");
    }
}
